<?php

namespace controllers\admin;

use libs\registry as Registry;
use libs\request as request;

class login extends \controllers\_SkyCMS_Controller
{
    
    /**
     * @route=(/login)
     */
    public function index()
    {
        $result = '';
        
        if(@request::post('zresetuj'))
        {
            $db = Registry::get('database');
            
            $user = $db->get_where('users',array('login'=>request::post('login'),
                                                'mail'=>request::post('mail')
                    ));
            
            if($user == null)
            {
                $result = '<span style="color:red;">Brak takiego użytkownika.</span>';
            }
            else
            {
                $result = '<span style="color:green;">Na Twoją skrzynkę wysłano maila.</span>';
                
                $ciag = substr(md5(rand()), 0, 12);
                
                $db->update('users',array('reset_char' => $ciag),
                                    array('login' => $user['login']));
                
                $this->resetujhaslo(request::post('mail'),$user);
            }
        }
        
        return array('result'=>$result);
    }
    
    /**
     * @route=(/login/result)
     */
    public function result()
    {
        if(request::post('haslo') && request::post('uzytkownik'))
        {
            $db = Registry::get('database');
            
            $haslo = md5('##'.request::post('haslo').'##');

            $user = $db->get_where('users',array('login'=>request::post('uzytkownik'),
                                                'haslo'=>$haslo
                    ));

            if($user != null) 
            {
                $_SESSION['loggedIn'] = true;
                $_SESSION['login'] = $user[0]['login'];
                $_SESSION['uprawnienia'] = explode(',',$user[0]['uprawnienia']);
                $_SESSION['poziom'] = $user[0]['poziom'];
                
                header("Location: ".URL.'_SkyCMS/');
            } 
        }
        
        header("Content-Type: text/html;charset=utf-8");
        return array('result'=>'Nie można się zalogować.');
        
    }
    
    
    /**
     * @route=(/out)
     */
    public function out()
    {
        session_destroy();
        header("Location: ".URL. '_SkyCMS');
    }
    
    
    /**
     * @route=(/reset/{id}&{ciagznakow}
     */
    public function reset($array)
    {
        list($id,$reset) = $array;
        
        $db = Registry::get('database');
        $user = $db->get_where('users',array('id'=>$id,
                                            'reset_char'=>$reset
        ));
        
        if($user == null)
        {
            header("Location: ".URL. '_SkyCMS');
        }
        
        $result = '';
        
        if(request::post('zmien_haslo'))
        {
            if(request::post('haslo') == request::post('haslo2'))
            {
                $haslo = md5('##'.request::post('haslo').'##');
                
                $db->update('users',array('haslo'=>$haslo,'reset_char'=>''),array('login'=>$user['login']));
                
                
                $result = '<span style="color:green;">Twoje hasło zostało zmienione.</span>';
            }
            else
            {
                $result = '<span style="color:red;">Podane hasła nie są takie same.</span>';
            }
        }
        
        return array('result'=>$result);
    }
    
    
    public function resetujhaslo($adres,$user)
    {
        
        $temat = "Wiadomość ze strony ".$domena;
        $alt_body = "Nadawca: ".$_POST['kto']."\nTelefon: ".$_POST['tel']."\nAdres e-mail: ".$_POST['mail']."\nWiadomość:\n".$tresc."\n\n--------------------------------------------------------------------\nWiadomość wysłana ze strony internetowej www.";
        $body = "<table>
            
        <tr><td>Kliknij w poniższy link, aby zresetować hasło:</td></tr>
        <tr><td><a href='".URL."_SkyCMS/reset/".$user['id']."&".$user['reset_char']."'>".URL."_SkyCMS/reset/".$user['id']."&".$user['reset_char']."</a></td></tr>
        
            
        </table><br>
        --------------------------------------------------------------------<br>
                                                               Wiadomość wysłana z Twojej strony internetowej";

        require_once("libs/mailer/PHPMailer.class.php");

        $mail = new \PHPMailer();
        $mail->SetLanguage( 'pl', '_SkyCMS/Class/PHPMailerPlugin/language/' );

        $mail->IsSMTP();         // set mailer to use SMTP
        $mail->SMTPSecure = 'tls';
        $mail->Host = "recomender.home.pl";  // specify main and backup server
        $mail->Port = 587;  // specify main and backup server
        $mail->SMTPAuth = true;     // turn on SMTP authentication
        $mail->Username = "admin@recomender.pl";  // SMTP username
        $mail->Password = "igia1962ewe69"; // SMTP password

        $mail->PluginDir = "admin/Class/PHPMailerPlugin/";
        $mail->From = '';

        $mail->FromName = $kto; //nagłówek From

        //$mail->SMTPDebug = true;

        $mail->Subject = $temat;

        // w zmienną $text_body wpisujemy treść maila


        $mail->AltBody = strip_tags($alt_body);
        $mail->Body = $body;
        // adresatów dodajemy poprzez metode 'AddAddress'
        $mail->AddAddress($adres);

        $mail->Send() or die($mail->ErrorInfo);
    
    }
}
